package org.tarena.netctoss.controller.role;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tarena.netctoss.dao.RoleMapperDao;
import org.tarena.netctoss.entity.Privilege;
import org.tarena.netctoss.entity.Role;
import org.tarena.netctoss.entity.RolePrivilege;
import org.tarena.netctoss.util.PrivilegeReader;

@Controller
@RequestMapping("/role")
public class RoleAddController {
	@Resource
	private RoleMapperDao dao;
	
	@RequestMapping(value="/toadd",
		method=RequestMethod.GET)
	@Transactional(readOnly=true)
	public String toAdd(Model model){
		//��ȡprivileges.xml�е�Ȩ����Ϣ
		List<Privilege> list = 
			PrivilegeReader.getModules();
		model.addAttribute("pris",list);
		//�������ҳ��
		return "role/role_add";
	}
	
	@RequestMapping(value="/add",
		method=RequestMethod.POST)
	@Transactional
	public String add(
		@RequestParam("name") String name,
		@RequestParam("pri") Integer[] priIds){
		//����daoд��role��
		Role role = new Role();
		role.setName(name);
		dao.addRole(role);
		//����daoд��role_privilege��
		for(Integer priId : priIds){
			RolePrivilege rp = new RolePrivilege();
			rp.setRole_id(role.getId());
			rp.setPrivilege_id(priId);
			dao.addRolePrivilege(rp);
		}
		return "redirect:/role/role_list/1";
	}
	
	
	
	
}
